package {	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.geom.Matrix;
	import flash.text.TextField;
	public class Person extends MovieClip {
				
		private var _type:uint;
		private var speed:uint = 3;
		private var state:uint = 0;
		private var _converted:Boolean;
		private var score:int;
		
		private var passedAway:Boolean;
		
		private var defaultOri = 1;
		
		private var game:Game;				
		private var colisionRate:uint;
		
		public function Person(game:Game, gameLevel:uint, type:uint = 0) {
			addEventListener(Event.ENTER_FRAME, enterFrame);
			
			this.game = game;						
			
			this._type = type;
			
			if (type == 0) {
				if ((int) (Math.random() * 4) == 0) {
					this._type = 3;
				}else {
					this._type = 2;
				}
				if ((int) (Math.random() * 7) == 0) this._type = 1;	
			}			
			
			cacheAsBitmap = true;
			cacheAsBitmapMatrix = new Matrix();
			
			gotoAndStop(this._type);
			
			if ( ((int) (Math.random() * 2)) == 0) defaultOri = -1;
			
			scaleX = defaultOri;
			
			speed = Math.min(14, (int) (2 + Math.random() * (2 + (gameLevel % 2))));
			
			speed += gameLevel % 2;
			
			if (this._type == 1) score = 100;
			else if (this._type == 2) score = 1 * (gameLevel + 1);
			else if (this._type == 3) score = 10 * (gameLevel +1);			
		}
		
		public function changeState(state:uint):void 
		{
			//if (_converted) return;
			if (passedAway) return;
			this.state = state;
		}
		
		private var colisionRateCooldown;
		
		private function enterFrame(e:Event):void 
		{
			//if (colisionRateCooldown == 0) colisionRate = 0;
			//else colisionRateCooldown--;		
			
			//if (colisionRate > 10) {
			//	speed++;
			//	colisionRate = 0;				
			//}
			
			if (state > 0) {
				gotoAndStop(this._type + 3);
				if (state == 1) scaleX = -1;
				else scaleX = 1;				
			}
			else {
				scaleX = defaultOri;
				gotoAndStop(this._type);
			}					
			
			checkMovement();					
		}
		
		private function checkMovement():void 
		{
			if (state == 0) y += speed;
			if (state == 1) x -= speed;
			if (state == 2) x += speed;
		}
		
		public function rightPassage():void 
		{		
			new RightSound().play();
			
			passedAway = true;
			
			removeEventListener(Event.ENTER_FRAME, enterFrame);					
			
			score *= game.combo;
			
			game.score += score;		
			
			if (_type == 3) {
				//game.balance -= 5;
				game.evilPower += 15;
			}
			else {
				game.balance += 2;
				game.goodPower += 5;
			}
			
			createText(0x00FF00, "+ " + score);
			
			addEventListener(Event.ENTER_FRAME, fadeAway);
		}		
		
		public function wrongPassage():void 
		{
			//new WrongSound().play();
			
			passedAway = true;
			
			removeEventListener(Event.ENTER_FRAME, enterFrame);					
			
			game.balance -= 10;
						
			person.gotoAndStop(5);
			var mc:MovieClip;
			if (type == 1) {
				if (state == 2) {
					mc = new GoodAnimDie();
					new FireSound().play();
				}else {
					mc = new EvilAnimDie();
					new CloudSound().play();
				}				 
			} 
			else if (type == 2) {
				 mc = new GoodAnimDie();
				 new FireSound().play();
			}
			else if (type == 3) {
				mc = new EvilAnimDie();
				new CloudSound().play();
			}
			mc.addEventListener(Event.ENTER_FRAME, die);
			addChild(mc);
					
 			
			/*game.score -= score;			
			
			createText(0xFF0000, "- " + score);*/						
		}	
		
		private function die(e:Event):void 
		{			
			var mc:MovieClip = e.currentTarget as MovieClip;
			person.alpha -= 0.2;
			if (mc.currentFrame >= mc.totalFrames) {
				mc.removeEventListener(Event.ENTER_FRAME, die);
				parent.removeChild(mc.parent);
			}
		}
		
		public function addColisionRate():void 
		{
			colisionRateCooldown = 20;
			colisionRate++;			
		}
		
		private function createText(color:int, score:String):void 
		{
			var text:ScoreText = new ScoreText();
			text.cacheAsBitmap = true;
			text.width = 50;
			text.x = x - this.width / 2;
			text.y = y;			
			text.text = score;
			text.textColor = color;
			parent.addChild(text)
		}
		
		private function fadeAway(e:Event):void 
		{
			var mc = e.currentTarget;
			mc.alpha -= 0.1;
			checkMovement();
			if (alpha <= 0) {
				mc.removeEventListener(Event.ENTER_FRAME, fadeAway);
				parent.removeChild(mc);
			}
		}
		
		public function get converted():Boolean 
		{
			return _converted;
		}
		
		public function set converted(value:Boolean):void 
		{
			_converted = value;
		}
		
		public function get type():uint 
		{
			return _type;
		}
		
		public function set type(value:uint):void 
		{
			_type = value;			
		}
	}
}
